using System.Collections.Generic;
using Dou.UI;
using UnityEngine;

namespace Simple
{
    public class Test_LoopList2 : MonoBehaviour
    {
        public List<Test_LoopListItemData> datas = new List<Test_LoopListItemData>();
        
        public LoopDataList loopList;

        private void Start()
        {
            // 填充数据
            for (int i = 0; i < 100; i++)
            {
                var item = new Test_LoopListItemData();
                item.text = (i + 1).ToString();
                for (int j = 0; j < 30; j++)
                {
                    var reward = new Test_LoopListItemRewardData();
                    reward.pic = j % 2 == 0 ? "tex_lt_icon_biaoqing_01" : "tex_lt_icon_biaoqing_02";
                    reward.hasEffect = j % 2 == 0;
                    item.rewardList.Add(reward);
                }
                datas.Add(item);
            }
            
            loopList.onItemInit += LoopListOnItemInit;
            loopList.onItemUpdate += LoopListOnItemUpdate;
            loopList.onItemDisable += LoopListOnItemDisable;
            loopList.dataProvider = new ListCollection<Test_LoopListItemData>(datas);
        }

        private void LoopListOnItemInit(GameObject go, object d, int index)
        {
            // 这里添加事件监听
            var list = go.transform.Find("RewardList").GetComponent<LoopDataList>();
            
            list.onItemInit += RewardOnItemInit;
            list.onItemUpdate += RewardOnItemUpdate;
            list.onItemDisable += RewardOnItemDestroy;
        }

        private void LoopListOnItemUpdate(GameObject go, object d, int index)
        {
            // 为了方便UI节点这里直接用 Find 来查找了
            var text = go.transform.Find("Lab").GetComponent<DTextMeshProUGUI>();
            var list = go.transform.Find("RewardList").GetComponent<LoopDataList>();
            
            var data = (Test_LoopListItemData) d;
            
            text.text = data.text;
            list.dataProvider = new ListCollection<Test_LoopListItemRewardData>(data.rewardList);
        }

        private void LoopListOnItemDisable(GameObject go, int index)
        {
            // 这里移除事件监听
            var list = go.transform.Find("RewardList").GetComponent<LoopDataList>();
            
            list.onItemInit -= RewardOnItemInit;
            list.onItemUpdate -= RewardOnItemUpdate;
            list.onItemDisable -= RewardOnItemDestroy;
        }

        private void RewardOnItemInit(GameObject go, object d, int index)
        {
        }

        private void RewardOnItemUpdate(GameObject go, object d, int index)
        {
            // 为了方便UI节点这里直接用 Find 来查找了
            var image = go.transform.Find("Image").GetComponent<DImage>();
            var eff = go.transform.Find("Eff").gameObject;
            
            var data = (Test_LoopListItemRewardData) d;
            
            // image.spriteName = data.pic;
            eff.SetActive(data.hasEffect);
        }

        private void RewardOnItemDestroy(GameObject go, int index)
        {
        }
    }
    
    public class Test_LoopListItemData
    {
        public string text;
        public List<Test_LoopListItemRewardData> rewardList = new List<Test_LoopListItemRewardData>();
    }

    public class Test_LoopListItemRewardData
    {
        public string pic;
        public bool hasEffect;
    }
}
